// stulist.js
var app = getApp()
import APIURL from '../../const/APIURL'
import STUDENT from '../../API/STUDENT'
import helper from '../..//common/helper'
import SPECIAL from '../../API/SPECIAL';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    student: null,
    students: [],
    tips: '最新报到情况',
    isloading: false,
    page: 1,
    stuname: '',
    stutzsh: '',
    studeptid: null,
    stuspeid: null,
    stuisbd: null,
    specials: [],
    specnames: [],
    specindex: 0,
    deptnames: [],
    deptindex: 0,
    bdlist: ['全部', '未报到', '已报到'],
    bdindex: 0,
    lastquery: null,
    currentquery: null,
    keyword: '',
    searchdeptid: '',
    searchspeid: '',
    searchisbd: '',
    totalcount: 0,
  },
  preparesearch: function () {
    var that = this;
    var data = that.data;
    if (!data.keyword)
    {
      data.keyword=''
    }
    if (!data.searchdeptid)
    {
      data.searchdeptid=''
    }
    if (!data.searchspeid)
    {
      data.searchspeid=''
    }
    if (!data.searchisbd && data.searchisbd!=0)
    {
      data.searchisbd=''
    }
    that.setData({
      page: 1,
      students: [],
      currentquery: {
        keyword: data.keyword,
        searchdeptid: data.searchdeptid,
        searchspeid: data.searchspeid,
        searchisbd: data.searchisbd
      }
    })
    that.tosearch()
  },
  tosearch: function () {
    var that = this
    var data = that.data.currentquery;
    var keyword = data.keyword;   
    var deptid = data.searchdeptid;
    var speid = data.searchspeid;
    var isbd = data.searchisbd;
    var total = data.totalcount;
    
    if (keyword && !isNaN(keyword)) {

      var searchword = that.data.searchword;
      that.getstudentsbytzsh(keyword)
    }
    else
      if (!keyword && !deptid && !speid && (!isbd && isbd.length == 0)) {        
        that.loadlateststudent()
      }
      else{
       var query={
          page:that.data.page,
          stuname:keyword,
          speid:speid,
          deptid:deptid,
          isbd:isbd
        }
        that.searchstudents(query)
      }
    
  },
  loadmore: function () {
    var that = this;
    
    if (that.data.students && that.data.students.length >= that.data.totalcount) {
      wx.showToast({
        title: '到底了!',
      })
      return;
    }
    if (that.data.isloading==true)
    {
      return;
    }
    this.setData({ page: that.data.page + 1 })
    that.tosearch()
  },
  searchinput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  getstudentsbytzsh: function (tzsh) {
    var that = this
    wx.showLoading({
      title: '搜索中!',
    })
    var query = { tzsh: tzsh, page: that.data.page }
    STUDENT.GetStudentsBytzsh(query).then(function (d) {

      wx.showToast({
        title: "获取成功",
      })

      that.setData({ students: that.data.students.concat(d.data), totalcount: d.dataCount, tips: '通知书号包含"' + tzsh + '"'})

    })
  },
  searchstudents:function(query){    
    var that = this
    wx.showLoading({
      title: '搜索中!',
    })    
    that.setData({
      isloading:true
    })        
    STUDENT.SearchStudents(query).then(function (d) {

      wx.showToast({
        title: "获取成功",
      })
      var msg='';
      if (query.keyword)
      {
        msg += '姓名包含"' + query.keyword+'"';
      }
      if (query.speid) {
        msg += '专业为"' + that.data.specnames[that.data.specindex] + '"';
      }
      that.setData({ students: that.data.students.concat(d.data), canloadmore: false, totalcount: d.dataCount, tips: msg, isloading:false })

    },function(d){
      that.setData({ isloading:false})
    })
  },
  getstudentbytzsh: function (tzsh) {
    var that = this
    wx.showLoading({
      title: '搜索中!',
    })
    var query = { tzsh: tzsh, page: that.data.page }
    STUDENT.GetStudent(query).then(function (d) {

      wx.showToast({
        title: "获取成功",
      })

      that.setData({ students: [].concat(d.data), canloadmore: false, totalcount: d.dataCount, tips: '扫描到通知书为"' + tzsh + '"' })

    })
  },
  getdeptId: function (index) {
    var deptnames = this.data.deptnames;
    if (!deptnames) {
      return null;
    }
    var deptname = deptnames[index];
    var d = this.data.specials;
    for (var i = 0; i < d.length; i++) {
      if (deptname == d[i].dept_Name) {
        return d[i].dept_Id
      }
    }
  },
  getspecialId: function (index) {
    var specnames = this.data.specnames;
    if (!specnames) {
      return null;
    }
    var specname = specnames[index];
    var d = this.data.specials;
    for (var i = 0; i < d.length; i++) {
      if (specname == d[i].spe_Name) {
        return d[i].spe_Id
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.hud = app.hudClass(this).hud;
    var specnames = wx.getStorageSync("specnames");
    var deptnames = wx.getStorageSync("deptnames");
    var specials = wx.getStorageSync("specials");
    if (!specnames || !deptnames || !specials) {
      this.loadspecials();
    }
    else {
      this.setData({
        specnames: specnames,
        deptnames: deptnames,
        specials: specials
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.preparesearch();
  },

  /**
   * 生命周期函数--监听页面显示
   */

  setquery: function (data) {
    this.setData({
      totalcount: data.totalcount
    })
  },
  bindBDPickerChange: function (e) {

    var index = e.detail.value;
    var that = this;
    var isbd = false;
    if (index == 0) {
      isbd = '';
    } else if (index == 1) {
      isbd = 0
    } else if (index == 2) {
      isbd = 1
    }

    this.setData({
      bdindex: index,
      searchisbd: isbd
    })
  },
  bindSpecPickerChange: function (e) {

    var index = e.detail.value;
    var that = this;

    var s = that.data.specials;
    if (index <= 0) {
      var deptindex = 0
    }
    else {
      var deptname = s[index - 1].dept_Name

      var d = that.data.deptnames;
      var deptindex = d.indexOf(deptname);

    }
    var speid = this.getspecialId(index)
    var deptid = this.getdeptId(deptindex)
    if (!speid)
    {
      speid=''
    }
    if (!deptid)
    {
      deptid=''
    }
    this.setData({
      specindex: index,
      deptindex: deptindex,
      searchspeid: speid,
      searchdeptid: deptid,      
    })
  },
  bindDeptPickerChange: function (e) {

    var index = e.detail.value;
    var that = this;
    var deptid = this.getdeptId(index)
    this.setData({
      deptindex: index,
      specindex: 0,
      searchdeptid: deptid
    })
  },
  loadlateststudent: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var data = { page: that.data.page };
    var that = this;
    STUDENT.GetLatestStudents(data).then(function (d) {


      var students = that.data.students.concat(d.data);
      that.setData({ students: students, totalcount: d.dataCount, tips: '最新报道情况' })
      wx.showToast({
        title: "加载成功",
      })
    }, function (d) {
      that.setData({ isloading: false })
      wx.hideLoading()
    });
  },
  onShow: function () {
    if (app.globalData.role != 'admin') {
      wx.showModal({
        title: '提示',
        content: '你好，游客不可以访问当前页面，点击返回！',
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '',
        success: function () {
          wx.switchTab({
            url: '/pages/department/department',
          })
        },
      })
      return;
    }
   
  },
  loadspecials: function () {
    var that = this;
    SPECIAL.GETSPECIALS().then(function (d) {
      if (d.responseResult != 1) {

        wx.showToast({
          title: d.msg ? '' : d.msg,
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        that.setData({
          showTopTips: true,
          errorMsg: d.msg
        })

        setTimeout(function () {
          that.setData({
            showTopTips: false,
            errorMsg: ''
          })
        }, 2000)
      }
      else {
        wx.showToast({
          title: "加载成功",
        })

        that.setData({
          specials: d.data
        })
        wx.setStorageSync("specials", d.data)
        that.preparedata();

      }
    }, function (d) {
      wx.hideLoading()
    })
  },
  preparedata: function () {
    var that = this;
    var d = that.data.specials;
    var specnames = ['全部'];
    var deptnames = ['全部'];
    for (var i = 0; i < d.length; i++) {
      specnames.push(d[i].spe_Name);
      if (deptnames.indexOf(d[i].dept_Name) == -1) {
        deptnames.push(d[i].dept_Name)
      }
    }
    that.setData({
      specnames: specnames,
      deptnames: deptnames
    })
    wx.setStorageSync("specnames", specnames)
    wx.setStorageSync("deptnames", deptnames)
    wx.setStorageSync("specials", d)
  },
  scancode: function () {
    var that = this
    this.setData({ isscanback: true })
    wx.scanCode({
      success: function (res) {
        that.getstudentbytzsh(res.result)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})